/**
 * Simple window manager; windows are extensions of Panels.
 * 
 * To be used with prana:
 * 
 *    <object id="windowManager" class="com.kusiri.rollsroyce.window.RrWindowManager"/>
 *    
 *    <object id="window" class="com.kusiri.window.Window" scope="prototype" lazy-init="true">
 *        <property name="windowManager">
 *            <ref>windowManager</ref>
 *        </property>    
 *    </object>
 * 
 * @see Panel
 * 
 */
package com.kusiri.window
{
	import com.kusiri.kframework.ui.IKfUiComponent;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.core.Container;
	import mx.core.IContainer;
	import mx.core.IFlexDisplayObject;
	import mx.effects.Fade;
	import mx.events.FlexEvent;
	
	public class WindowManager
	{
		private const FADE_TIME:int = 300;
		private const FADE_DELAY:int = 0;
		
		[Bindable]
		protected var minHeight:int = 100;
		
		[Bindable]
		protected var minWidth:int = 100;
		
		protected var _container:IContainer;
		protected var _windowCanvas:Canvas;
		protected var fade:Fade;
		
		protected var windows:ArrayCollection = new ArrayCollection();
		
		
		public function WindowManager()
		{
		
		}
		

		
		/**
		 * @param container The window container
		 */
		public function init(container:IContainer, callback:Function) : void
		{	
			_container = container;
			_windowCanvas = new Canvas();
			//_windowCanvas.percentHeight = 100;
			_windowCanvas.percentWidth = 100;
			_windowCanvas.setStyle('x', 0);
			_windowCanvas.setStyle('y', 0);
			_windowCanvas.horizontalScrollPolicy = "off";
			_windowCanvas.verticalScrollPolicy = "off";

			_container.addChild(_windowCanvas);
			_windowCanvas.addEventListener(FlexEvent.CREATION_COMPLETE, callback, false, 0, true);
			
		}
		
		public function getContainingWindow(o:IFlexDisplayObject) : Window
		{
			var p:DisplayObjectContainer = o.parent;
			while (p)
			{
				if (p is Window) return Window(p);
				p = p.parent;	
			}
			return null;
		}
		
		/**
		 * @return The window container
		 */
		public function get container() : IContainer
		{
			return _container;
		}
		
		/**
		 * @return The window container
		 */
		public function get windowCanvas() : Canvas
		{
			return _windowCanvas;
		}


		/**
		 * @param name Name of the window
		 * @return The window or null
		 */
		public function getWindowByName(name:String) : Window
		{
			var o:Object = _windowCanvas.getChildByName(name);
			return o is Window ? o as Window : null;
		}
		
		/**
		 * @param name Name of the window
		 */
		public function namedWindowExists(name:String) : Boolean
		{
			return getWindowByName(name) ? true : false;
		}

		/**
		 * @param name Name of the window
		 */
		public function removeWindowByName(name:String) : void
		{
			var w:Window = getWindowByName(name)
			if (w == null) return;
			removeWindow(w);
		}
		
		/**
		 * @param window The window
		 */
		public function removeWindow(window:Window) : void
		{	
			window.kfUiComponent.__deactivate();
			_windowCanvas.removeChild(window); 
			if (_windowCanvas.numChildren > 0) select(Window(_windowCanvas.getChildAt(_windowCanvas.numChildren - 1)));
		}
		
		public function focus( window : Window ) : void
		{
			var topWindowIndex : int = _windowCanvas.numChildren - 1;
			var windowIndex : int = _windowCanvas.getChildIndex( window );			
			
			if ( windowIndex < 0 )
			{
				return;
			}
			
			if ( topWindowIndex > 0 ) 
			{
				if (windowIndex == topWindowIndex) 
				{
					// already top window, just do styling
					topWindowIndex--;
				}
				else
				{
					_windowCanvas.setChildIndex(window, topWindowIndex);
				}
				
				for (var i:int=0; i<=topWindowIndex; i++)
				{
					var o:Object = _windowCanvas.getChildAt(i);
					deselect(o as Window);
				}
			}

			select(window);
		}
		
		protected function deselect(window:Window) : void
		{
			if (window.styleName != '')
			{
				window.styleName = '';
				window.alpha = 0.6;
			}
		}
		
		protected function select(window:Window) : void
		{
			if (window.styleName != 'PanelSelected')
			{
				window.styleName = 'PanelSelected';
				window.alpha = 1;
			}
		}
		
		
		/**
		 * @param window Add this window to the view area
		 */
		protected function addWindow( window : Window, content : IFlexDisplayObject):void
		{	
			windowCanvas.addChild(window);
			focus(window);
			window.addChild(content as DisplayObject);
		}

		public function newWindow( kfUiComponent : IKfUiComponent, title : String, content : IFlexDisplayObject, width : Number =  undefined, height : Number = undefined, x : int = 500, y : int = 0, resizable : Boolean = true, showMaximize : Boolean = true, showClose : Boolean = true, name : String = undefined ) : Window
		{
			
			var window:Window = _newWindow(kfUiComponent, title, width, height, x, y, resizable, showMaximize, showClose, name);
			addWindow(window, content);	
			return window;
		}
		
		public function _newWindow( kfUiComponent : IKfUiComponent, title : String, width : Number =  undefined, height : Number = undefined, x : int = 500, y : int = 200, resizable : Boolean = true, showMaximize : Boolean = true, showClose : Boolean = true, name : String = undefined ) : Window
		{
			
			var window:Window = new Window();
			window.kfUiComponent = kfUiComponent;
			window.windowManager = this;
			window.showClose = showClose;
			window.showMaximize = showMaximize;
			window.enableResize = resizable;	
			window.title = title;
			if (width) window.width = width;
			if (height) window.height = height;
			if (name) window.name = name;
			window.x = x;
			window.y = y;
			
			return window;
			
		}
		

	}
}